package com.geniusk.system.service.impl;

import com.geniusk.system.model.mapper.ModelPropertyMapper;
import com.geniusk.system.model.mapper.PropertyEventMapper;
import com.geniusk.system.model.pojo.PropertyEvent;
import com.geniusk.system.service.PropertyEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PropertyEventServiceImpl implements PropertyEventService {

    @Autowired
    private PropertyEventMapper propertyEventMapper;

    @Autowired
    private ModelPropertyMapper modelPropertyMapper;

    @Override
    public int insertPropertyEvent(Long eventId, Long[] propertyIds) {
        propertyEventMapper.deletePropertyEventByEventId(eventId);
        if (propertyIds != null && propertyIds.length > 0) {
            //判断角色是否存在
            boolean isExist = modelPropertyMapper.selectPropertyIsExistByIds(propertyIds);
            if (isExist) {
                // 新增用户与角色管理
                List<PropertyEvent> list = new ArrayList<>();
                for (Long propertyId : propertyIds) {
                    PropertyEvent propertyEvent = new PropertyEvent();
                    propertyEvent.setModelEventId(eventId);
                    propertyEvent.setModelPropertyId(propertyId);
                    list.add(propertyEvent);
                }
                return propertyEventMapper.insertPropertyEvent(list);
            }
        }
        return 0;
    }
}
